﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlTypes;

namespace NGinnBPM.Services.Utilities
{
    public class SqlDBUtil
    {
        public static void AddParameter(IDbCommand cmd, string name, string value)
        {
            IDbDataParameter prm = cmd.CreateParameter();
            prm.ParameterName = name;
            prm.Value = value == null ? SqlString.Null : value;
            prm.DbType = DbType.String;
            cmd.Parameters.Add(prm);
        }

        public static void AddParameter(IDbCommand cmd, string name, int? value)
        {
            IDbDataParameter prm = cmd.CreateParameter();
            prm.ParameterName = name;
            prm.Value = value.HasValue ? value.Value : SqlInt32.Null;
            prm.DbType = DbType.Int32;
            cmd.Parameters.Add(prm);
        }

        public static void AddParameter(IDbCommand cmd, string name, DateTime? value)
        {
            IDbDataParameter prm = cmd.CreateParameter();
            prm.ParameterName = name;
            prm.Value = value.HasValue ? value.Value : SqlDateTime.Null;
            prm.DbType = DbType.DateTime;
            cmd.Parameters.Add(prm);
        }

        public static void AddParameter(IDbCommand cmd, string name, byte[] value)
        {
            IDbDataParameter prm = cmd.CreateParameter();
            prm.ParameterName = name;
            prm.Value = value;
            prm.DbType = DbType.Binary;
            cmd.Parameters.Add(prm);
        }
    }
}
